//
//  WaterRecordController.m
//  ForestPack
//
//  Created by 骆锴 on 2018/8/3.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "WaterRecordController.h"
#import "ApplyBillViewController.h"
#import "WaterRecordModel.h"
#import "WaterRecodCell.h"

@interface WaterRecordController ()<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView *moveLine;
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, assign) NSInteger       type;

@property (nonatomic,strong)  NSMutableArray  * alldataArray;
@property (nonatomic,strong)  NSMutableArray  * incomedataArray;
@property (nonatomic,strong)  NSMutableArray  * costdataArray;

@property (nonatomic,assign)  NSInteger       allpage;
@property (nonatomic,assign)  NSInteger       incomepage;
@property (nonatomic,assign)  NSInteger       costpage;
@property (nonatomic,assign)  NSInteger       allCurrent_page;
@property (nonatomic,assign)  NSInteger       incomeCurrent_page;
@property (nonatomic,assign)  NSInteger       costCurrent_page;

@end

@implementation WaterRecordController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setShadowImage:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitle:@"明细"];
    self.type = 0;
    self.alldataArray = [NSMutableArray array];
    self.incomedataArray = [NSMutableArray array];
    self.costdataArray = [NSMutableArray array];
    [self setTopBar];
    [self setUI];
}

- (void)setTopBar {
    UIView *topBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(30))];
    topBar.backgroundColor = RGB_COLOR(235, 67, 90);
    [self.view addSubview:topBar];

    NSArray *nameArr = @[@"全部",@"收入",@"支出"];
    for (int i = 0; i < nameArr.count; i++) {
        UIButton *typeBtn = [[UIButton alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH / 3, 0, SCREEN_WIDTH / 3, ScreenX375(30))];
        [typeBtn setTitle:nameArr[i] forState:UIControlStateNormal];
        [typeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [typeBtn setTitleColor:RGBACOLOR(255, 255, 255, 0.6) forState:UIControlStateNormal];
        typeBtn.titleLabel.font = Kfont(16);
        typeBtn.tag = 100 + i;
        if (i == 0) {
            typeBtn.selected = YES;
            _selectBtn = typeBtn;
        }
        [typeBtn addTarget:self action:@selector(typeClick:) forControlEvents:UIControlEventTouchUpInside];
        [topBar addSubview:typeBtn];
    }

    self.moveLine = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(30) - 2, SCREEN_WIDTH / 3, 2)];
    self.moveLine.backgroundColor = [UIColor whiteColor];
    [topBar addSubview:self.moveLine];
}

- (void)typeClick:(UIButton *)button {
    _selectBtn.selected = NO;
    button.selected = YES;
    _selectBtn = button;
    _type = button.tag - 100;
    if (button.tag == 101 && _incomedataArray.count == 0) {
        BaseTableView *tableView = [self.view viewWithTag:201];
        [tableView.mj_header beginRefreshing];
    }else if(button.tag == 102 && _costdataArray.count == 0){
        BaseTableView *tableView = [self.view viewWithTag:202];
        [tableView.mj_header beginRefreshing];
    }

    [UIView animateWithDuration:0.3 animations:^{
        self.moveLine.frame = CGRectMake((button.tag - 100) * SCREEN_WIDTH / 3, ScreenX375(30) - 2, SCREEN_WIDTH / 3, 2);
    }];
    [_scrollView setContentOffset:CGPointMake((button.tag - 100) * SCREEN_WIDTH, 0) animated:YES];
}

- (void)setUI {
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, ScreenX375(30), SCREEN_WIDTH, SCREEN_HEIGHT - ScreenX375(30))];
    _scrollView.showsVerticalScrollIndicator = false;
    _scrollView.showsHorizontalScrollIndicator = false;
    _scrollView.pagingEnabled = true;
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 0);
    [self.view addSubview:_scrollView];

    for (int i = 0; i < 3; i++) {
        BaseTableView *tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(30))];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        tableView.tag = 200 + i;

        [_scrollView addSubview:tableView];

        __weak typeof(self) weakSelf = self;
        tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            if (weakSelf.type == 0 ) {
                weakSelf.allCurrent_page = 1;
            }else if(weakSelf.type == 1){
                weakSelf.incomeCurrent_page = 1;
            }else{
                weakSelf.costCurrent_page = 1;
            }

            NSString *url = [NSString stringWithFormat:@"%@app/wallet_record",REQUESTHEADER];
            if (weakSelf.type == 0) {
                url = [NSString stringWithFormat:@"%@app/wallet_record",REQUESTHEADER];
            }

            NSDictionary *parameter = [[NSDictionary alloc] init];
            if (weakSelf.type == 0) {
                parameter = @{@"token":[FPLoginAndRegisterTools getToken],@"page":@"1"};
            }else if (weakSelf.type == 1){
                parameter = @{@"token":[FPLoginAndRegisterTools getToken],@"page":@"1",@"type":@"1"};
            }else{
                parameter = @{@"token":[FPLoginAndRegisterTools getToken],@"page":@"1",@"type":@"2"};
            }
            NSLog(@"%@",parameter);

            [HttpManager postHttpRequestByPost:url andParameter:parameter success:^(id successResponse) {
                NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
                if ([status isEqualToString:@"1"]) {
                    [tableView.mj_header endRefreshing];
                    NSLog(@"%@",successResponse);
                    NSError *error;
                    if (weakSelf.type == 0) {
                        [self.alldataArray removeAllObjects];
                        self.allpage = [successResponse[@"data"][@"last_page"] integerValue];
                        for (NSDictionary *dic in successResponse[@"data"][@"data"]) {
                            WaterRecordModel *model = [[WaterRecordModel alloc] initWithDictionary:dic error:&error];
                            if (model != nil) {
                                [self.alldataArray addObject:model];
                            }
                            NSLog(@"%@",model);
                        }
                    }else if(weakSelf.type == 1){
                        [self.incomedataArray removeAllObjects];
                        self.incomepage = [successResponse[@"data"][@"last_page"] integerValue];
                        for (NSDictionary *dic in successResponse[@"data"][@"data"]) {
                            WaterRecordModel *model = [[WaterRecordModel alloc] initWithDictionary:dic error:&error];
                            if (model != nil) {
                                [self.incomedataArray addObject:model];
                            }
                        }
                    }else{
                        [self.costdataArray removeAllObjects];
                        self.costpage = [successResponse[@"data"][@"last_page"] integerValue];
                        for (NSDictionary *dic in successResponse[@"data"][@"data"]) {
                            WaterRecordModel *model = [[WaterRecordModel alloc] initWithDictionary:dic error:&error];
                            if (model != nil) {
                                [self.costdataArray addObject:model];
                            }
                        }
                    }
                    [tableView reloadData];
                }else{
                    [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
                }
            } andFailure:^(id failureResponse) {
                [tableView.mj_header endRefreshing];
                [HttpManager showFail];
            }];
        }];

        tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            if (weakSelf.type == 0) {
                weakSelf.allCurrent_page++;
            }else if(weakSelf.type == 1){
                weakSelf.incomeCurrent_page++;
            }else{
                weakSelf.costCurrent_page++;
            }
        }];

        if (i == 0) {
            [tableView.mj_header beginRefreshing];
        }
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ScreenX375(100);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView.tag == 200) {
        return _alldataArray.count;
    }else if (tableView.tag == 201){
        return _incomedataArray.count;
    }else{
        return _costdataArray.count;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag == 200) {
        WaterRecodCell *cell = [WaterRecodCell cellWithTableView:tableView andIndexPath:indexPath];
        WaterRecordModel *model = [self.alldataArray objectAtIndex:indexPath.section];
        [cell initWithModel:model];
        return cell;
    }else if(tableView.tag == 201){
        WaterRecodCell *cell = [WaterRecodCell cellWithTableView:tableView andIndexPath:indexPath];
        WaterRecordModel *model = [self.incomedataArray objectAtIndex:indexPath.section];
        [cell initWithModel:model];
        return cell;
    }else{
        WaterRecodCell *cell = [WaterRecodCell cellWithTableView:tableView andIndexPath:indexPath];
        WaterRecordModel *model = [self.costdataArray objectAtIndex:indexPath.section];
        [cell initWithModel:model];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return ScreenX375(10);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(10))];
    view.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
    return view;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == _scrollView) {
        NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;

        UIButton *button = [self.view viewWithTag:index + 100];
        _selectBtn.selected = NO;
        button.selected = YES;
        _selectBtn = button;
        [UIView animateWithDuration:0.3 animations:^{
            self.moveLine.frame = CGRectMake((button.tag - 100) * SCREEN_WIDTH / 3, ScreenX375(30) - 2, SCREEN_WIDTH / 3, 2);
        }];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;

    UIButton *button = [self.view viewWithTag:index + 100];
    _selectBtn.selected = NO;
    button.selected = YES;
    _selectBtn = button;
    _type = button.tag - 100;
    if (button.tag == 101 && _incomedataArray.count == 0) {
        BaseTableView *tableView = [self.view viewWithTag:201];
        [tableView.mj_header beginRefreshing];
    }else if (button.tag == 103 && _costdataArray.count == 0){
        BaseTableView *tableView = [self.view viewWithTag:202];
        [tableView.mj_header beginRefreshing];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
